package com.sjp.entity;

import javax.swing.*;
import java.util.*;
import java.util.Map;
import java.util.stream.Collectors;


public class EleBox {
    private static List<Brand> slot = new ArrayList<>();

    public void add(Brand brand){
        slot.add(brand);
        //名称对集合的排序
        slot.sort(Comparator.comparing(Brand::getName));
        Map<String ,List<Brand>> map = slot.stream().collect(Collectors.groupingBy(Brand::getName));
        Set<String> set = map.keySet();
        for(String s : set){
            List<Brand> brandList = map.get(s);
            if (brandList.size()==3){
                deleteByBrandName(s);
                break;
            }
        }
        paint();
        over(brand);
    }
    public void deleteByBrandName(String name){
        Iterator<Brand> iterator = slot.iterator();
        while (iterator.hasNext()){
            Brand b = iterator.next();
            if (b.getName().equals(name)){
                b.getParent().remove(b);
                iterator.remove();
            }

        }
    }

    public void paint(){
        for (int i = 0; i < slot.size(); i++) {
            Brand brand = slot.get(i);
            int x  = i *brand.getWidth()+10;
            brand.setBounds(x,500,50,50);
        }
    }

    public void over(Brand brand){
        if (slot.size()>=7){
            JOptionPane.showConfirmDialog(brand,"游戏结束");
            System.exit(0);
        }
    }
}
